﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using Microsoft.Deployment.WindowsInstaller;
using Moq;
using wpfsetup.Setup.Model;
using wpfsetup.Setup.ViewModel;
using Xunit;

namespace wpfsetup.Tests.ViewModel
{
    public class ExternalUIHandlerViewModelTests
    {
        [Fact]
        public void Installer_InternalUI_is_set_to_None_SourcesOnly()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            mockInstallerAdapter.Verify(m => m.SetInternalUI(InstallUIOptions.Silent | InstallUIOptions.SourceResolutionOnly), Times.AtLeastOnce());
        }

        [Fact]
        public void Installer_ExternalUI_InstallUIOptions_are_set()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            mockInstallerAdapter.Verify(
                m =>
                m.SetExternalUI(It.IsAny<ExternalUIHandler>(),
                                InstallLogModes.Progress |
                                InstallLogModes.FatalExit |
                                InstallLogModes.Error |
                                InstallLogModes.Warning |
                                InstallLogModes.User |
                                InstallLogModes.Info |
                                InstallLogModes.ResolveSource |
                                InstallLogModes.OutOfDiskSpace |
                                InstallLogModes.ActionStart |
                                InstallLogModes.ActionData |
                                InstallLogModes.CommonData |
                                InstallLogModes.Initialize |
                                InstallLogModes.Terminate |
                                InstallLogModes.ShowDialog));
        }

        [Fact]
        public void BasicUIHandler_matches_ExternalUIHandler_signature()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            ExternalUIHandler externalUIHandler = externalUIHandlerViewModel.BasicUIHandler;
            Assert.Equal(externalUIHandler, externalUIHandlerViewModel.BasicUIHandler);
        }

        [Fact]
        public void BasicUIHandler_sets_IntenalUI_to_Basic_only_once()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Error, "", MessageButtons.OK,
                                                      MessageIcon.Error, MessageDefaultButton.Button1);
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Error, "", MessageButtons.OK,
                                                      MessageIcon.Error, MessageDefaultButton.Button1);
            mockInstallerAdapter.Verify(m => m.SetInternalUI(InstallUIOptions.Basic), Times.Exactly(1));
        }

        [Fact]
        public void BasicUIHandler_returns_MessageResultNone_if_the_message_is_empty()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            var message = "";
            var returnedMessageResult = externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Error, message,
                                                                                  MessageButtons.OK, MessageIcon.Error,
                                                                                  MessageDefaultButton.Button1);
            Assert.Equal(MessageResult.None, returnedMessageResult);
        }

        [Fact(Skip = "Actually shows the window, so we want to skip this for automated testing.")]
        public void BasicUIHandler_shows_User32MessageBox()
        {
            var ia = new Mock<IInstallerAdapter>();
            var vm = new ExternalUIHandlerViewModel(ia.Object);
            var message = "Error message text here.";
            var returnedMessageResult = vm.BasicUIHandler(InstallMessage.FatalExit,
                                                          message,
                                                          MessageButtons.RetryCancel,
                                                          MessageIcon.Information,
                                                          MessageDefaultButton.Button3);
            switch (returnedMessageResult)
            {
                case MessageResult.Error:
                    Debug.WriteLine("Error");
                    break;
                case MessageResult.None:
                    Debug.WriteLine("None");
                    break;
                case MessageResult.OK:
                    Debug.WriteLine("OK");
                    break;
                case MessageResult.Cancel:
                    Debug.WriteLine("Cancel");
                    break;
                case MessageResult.Abort:
                    Debug.WriteLine("Abort");
                    break;
                case MessageResult.Retry:
                    Debug.WriteLine("Retry");
                    break;
                case MessageResult.Ignore:
                    Debug.WriteLine("Ignore");
                    break;
                case MessageResult.Yes:
                    Debug.WriteLine("Yes");
                    break;
                case MessageResult.No:
                    Debug.WriteLine("No");
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        [Fact]
        public void BasicUIHandler_disables_ActionData_when_the_message_type_is_ActionStart()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            externalUIHandlerViewModel.EnableActionData = true;
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.ActionStart, "Message Text", MessageButtons.OK,
                                                      MessageIcon.Error, MessageDefaultButton.Button1);
            Assert.False(externalUIHandlerViewModel.EnableActionData);
        }

        [Fact]
        public void ProgressTotal_begins_at_zero()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            Assert.Equal(0, externalUIHandlerViewModel.ProgressTotal);
        }

        [Fact]
        public void BasicUIHandler_returns_MessageResultOK_when_message_type_is_ActionData()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            var result = externalUIHandlerViewModel.BasicUIHandler(InstallMessage.ActionData, "Message Text",
                                                                   MessageButtons.OK, MessageIcon.Error,
                                                                   MessageDefaultButton.Button1);


            Assert.Equal(MessageResult.OK, result);
        }

        [Fact]
        public void BasicUIHandler_sets_ActionDataText_when_message_type_is_ActionData_but_only_if_ProgressTotal_has_been_initialized()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            var message = "Message Text";
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.ActionData, message, MessageButtons.OK,
                                                      MessageIcon.Error, MessageDefaultButton.Button1);
            Assert.NotEqual(message, externalUIHandlerViewModel.ActionDataText);
            externalUIHandlerViewModel.ProgressTotal = 100;
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.ActionData, message, MessageButtons.OK,
                                                      MessageIcon.Error, MessageDefaultButton.Button1);
            Assert.Equal(message, externalUIHandlerViewModel.ActionDataText);
        }

        [Fact]
        public void BasicUIHandler_ProgressMessage_returns_MessageResultOK_for_invalid_message_strings()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            var message = "1: 0 2: 100 3:";
            var result = externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Progress, message, MessageButtons.OK,
                                                      MessageIcon.Error, MessageDefaultButton.Button1);
            Assert.Equal(MessageResult.OK, result);
        }

        [Fact]
        public void BasicUIHandler_ProgressMessage_resets_progress_bar_and_sets_the_expected_number_of_ticks()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            // Field 1: 0
            // Field 2: ProgresTotal
            // Field 3: Forward (0) or Backward (1) Progress
            // Field 4: Execution is in progress (0) or Creating script, please wait (1)
            var message = "1: 0 2: 100 3: 0 4: 0";
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Progress, message, MessageButtons.OK,
                                                      MessageIcon.Error, MessageDefaultButton.Button1);
            Assert.Equal(100, externalUIHandlerViewModel.ProgressTotal);
            Assert.Equal(FlowDirection.LeftToRight, externalUIHandlerViewModel.ProgressDirection);
            Assert.Equal(0, externalUIHandlerViewModel.Progress);
            Assert.Equal(true, externalUIHandlerViewModel.InProgress);

            message = "1: 0 2: 100 3: 1 4: 1";
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Progress, message, MessageButtons.OK,
                                                      MessageIcon.Error, MessageDefaultButton.Button1);
            Assert.Equal(FlowDirection.RightToLeft, externalUIHandlerViewModel.ProgressDirection);
            Assert.Equal(externalUIHandlerViewModel.ProgressTotal, externalUIHandlerViewModel.Progress);
        }

        [Fact]
        public void BasicUIHandler_ProgressMessage_provides_information_for_ActionData_messages()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            // Field 1: 1
            // Field 2: Number of ticks the progress bar moves for each ActionData message that is sent by the current action.
            //          This field is ignored if Field 3 is 0.
            // Field 3: Action will send ProgressReport messages (0) or Action will send ActionData messages that include progress (1).
            externalUIHandlerViewModel.EnableActionData = true;
            var message = "1: 1 2: 5 3: 0";
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Progress, message, MessageButtons.OK,
                                                      MessageIcon.Error, MessageDefaultButton.Button1);
            Assert.False(externalUIHandlerViewModel.EnableActionData);
            message = "1: 1 2: 5 3: 1";
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Progress, message, MessageButtons.OK,
                                                      MessageIcon.Error, MessageDefaultButton.Button1);
            Assert.Equal(5, externalUIHandlerViewModel.ActionDataProgress);
            Assert.True(externalUIHandlerViewModel.EnableActionData);
        }

        [Fact]
        public void BasicUIHandler_ActionData_increments_Progress_if_EnableActionData_is_true()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            externalUIHandlerViewModel.EnableActionData = true;
            externalUIHandlerViewModel.ActionDataProgress = 5;
            externalUIHandlerViewModel.Progress = 10;
            externalUIHandlerViewModel.ProgressTotal = 100;
            externalUIHandlerViewModel.ProgressDirection = FlowDirection.LeftToRight;
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.ActionData, "Message Text", MessageButtons.OK,
                                                      MessageIcon.Error, MessageDefaultButton.Button1);
            Assert.Equal(15, externalUIHandlerViewModel.Progress);
        }

        [Fact]
        public void BasicUIHandler_ActionData_decrements_Progress_if_EnableActionData_is_true()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            externalUIHandlerViewModel.EnableActionData = true;
            externalUIHandlerViewModel.ActionDataProgress = 5;
            externalUIHandlerViewModel.Progress = 10;
            externalUIHandlerViewModel.ProgressTotal = 100;
            externalUIHandlerViewModel.ProgressDirection = FlowDirection.RightToLeft;
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.ActionData, "Message Text", MessageButtons.OK,
                                                      MessageIcon.Error, MessageDefaultButton.Button1);
            Assert.Equal(5, externalUIHandlerViewModel.Progress);
        }

        [Fact]
        public void BasicUIHandler_ProgressMessage_increments_the_progress_bar_only_if_ProgressTotal_has_been_initialized()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            // Field 1: 2
            // Field 2: Number of ticks the progress bar has moved.
            var message = "1: 2 2: 5";
            externalUIHandlerViewModel.Progress = 0;
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Progress, message, MessageButtons.OK,
                                                      MessageIcon.Error, MessageDefaultButton.Button1);
            Assert.NotEqual(5, externalUIHandlerViewModel.Progress);
            externalUIHandlerViewModel.ProgressTotal = 100;
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Progress, message, MessageButtons.OK,
                                          MessageIcon.Error, MessageDefaultButton.Button1);
            Assert.Equal(5, externalUIHandlerViewModel.Progress);
        }

        [Fact]
        public void BasicUIHandler_ProgressMessage_decreases_the_progress_bar()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            // Field 1: 2
            // Field 2: Number of ticks the progress bar has moved.
            var message = "1: 2 2: 5";
            externalUIHandlerViewModel.Progress = 15;
            externalUIHandlerViewModel.ProgressTotal = 100;
            externalUIHandlerViewModel.ProgressDirection = FlowDirection.RightToLeft;
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Progress, message, MessageButtons.OK,
                                                      MessageIcon.Error, MessageDefaultButton.Button1);
            Assert.Equal(10, externalUIHandlerViewModel.Progress);
        }

        [Fact]
        public void BasicUIHandler_ProgressMessage_increments_ProgressTotal()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            // Field 1: 3
            // Field 2: Number of ticks to add to the expected total.
            var message = "1: 3 2: 5";
            externalUIHandlerViewModel.ProgressTotal = 100;
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Progress, message, MessageButtons.OK,
                                                      MessageIcon.Error, MessageDefaultButton.Button1);
            Assert.Equal(105, externalUIHandlerViewModel.ProgressTotal);
        }

        [Fact]
        public void CancellInstall_is_initalized_to_false()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            Assert.False(externalUIHandlerViewModel.CancelInstall);
        }

        [Fact]
        public void BasicUIHandler_ProgressMessage_returns_MessageResultCancel_if_CancelInstall_is_true()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            externalUIHandlerViewModel.CancelInstall = true;
            var result = externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Progress, "Message Text",
                                                                   MessageButtons.OK, MessageIcon.None,
                                                                   MessageDefaultButton.Button1);
            Assert.Equal(MessageResult.Cancel, result);
        }

        [Fact]
        public void BasicUIHandler_returns_MessageResultOK_when_message_type_is_CommonData()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            var result = externalUIHandlerViewModel.BasicUIHandler(InstallMessage.CommonData, "Message Text", MessageButtons.OK,
                                                      MessageIcon.None, MessageDefaultButton.Button1);
            Assert.Equal(MessageResult.OK, result);
        }


        [Fact]
        public void BasicUIHandler_CommonData_sets_the_LangID()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            // Field 1 = 0
            // Field 2 = LANGID
            // Field 3 = CodePage - we're ignoring this value.
            var message = "1: 0 2: 1033 3: ?";
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.CommonData, message, MessageButtons.OK,
                                                      MessageIcon.None, MessageDefaultButton.Button1);
            Assert.Equal(1033, externalUIHandlerViewModel.LangID);
        }

        [Fact]
        public void BasicUIHandler_CommonData_sets_the_Caption()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            // Field 1 = 1
            // Field 2 = CAPTION
            var message = "1: 1 2: Caption";
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.CommonData, message, MessageButtons.OK,
                                                      MessageIcon.None, MessageDefaultButton.Button1);
            Assert.Equal("Caption", externalUIHandlerViewModel.Caption);
        }

        [Fact]
        public void BasicUIHandler_CommonData_enables_CancelInstall()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            // Field 1 = 2
            // Field 2 = 0 (Disable Cancel button) or 1 (Enable Cancel button)
            var message = "1: 2 2: 0";
            externalUIHandlerViewModel.EnableCancelInstall = true;
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.CommonData, message, MessageButtons.OK,
                                                      MessageIcon.None, MessageDefaultButton.Button1);
            Assert.False(externalUIHandlerViewModel.EnableCancelInstall);
        }

        [Fact]
        public void BasicUIHandler_CommonData_disables_CancelInstall()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            // Field 1 = 2
            // Field 2 = 0 (Disable Cancel button) or 1 (Enable Cancel button)
            var message = "1: 2 2: 1";
            externalUIHandlerViewModel.EnableCancelInstall = false;
            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.CommonData, message, MessageButtons.OK,
                                                      MessageIcon.None, MessageDefaultButton.Button1);
            Assert.True(externalUIHandlerViewModel.EnableCancelInstall);
        }

        [Fact]
        public void BasicUIHandler_returns_MessageResultOK_when_message_type_is_Initialize()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            var result = externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Initialize, "Message Text", MessageButtons.OK,
                                                      MessageIcon.None, MessageDefaultButton.Button1);
            Assert.Equal(MessageResult.OK, result);
        }

        [Fact]
        public void BasicUIHandler_returns_MessageResultOK_when_message_type_is_Terminate()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            var result = externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Terminate, "Message Text", MessageButtons.OK,
                                                      MessageIcon.None, MessageDefaultButton.Button1);
            Assert.Equal(MessageResult.OK, result);
        }

        [Fact]
        public void BasicUIHandler_returns_MessageResultOK_when_message_type_is_ShowDialog()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            var result = externalUIHandlerViewModel.BasicUIHandler(InstallMessage.ShowDialog, "Message Text", MessageButtons.OK,
                                                      MessageIcon.None, MessageDefaultButton.Button1);
            Assert.Equal(MessageResult.OK, result);
        }

        [Fact]
        public  void BasicUIHandler_returns_MessgeResultNone_when_messge_type_is_other()
        {
            var mockInstallerAdapter = new Mock<IInstallerAdapter>();
            var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
            var result = externalUIHandlerViewModel.BasicUIHandler(InstallMessage.InstallStart, "Message Text", MessageButtons.OK,
                                                      MessageIcon.None, MessageDefaultButton.Button1);
            Assert.Equal(MessageResult.None, result);
        }
    }
}
